package com.home.truc.code.components;

import java.awt.MouseInfo;
import java.awt.Point;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.home.Common.Common;
import com.home.Entity.Ban;
import com.home.Entity.KhuVucBan;
import com.home.duy.code.DAO.BanDAO;
import com.home.truc.GUI.frmChuyenBan;
import com.home.truc.code.process.MyControl;

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;

public class MyJPanelKhuVucBan extends JPanel {
	private static final long serialVersionUID = 1L;
	boolean press = false;
	MyItemBan oldItem = null;//luu lai vi tri ban khi nhan chuot, dung de ghep ban
	MyItemBan newitem = null;
	infoCursor info = new infoCursor();
	boolean ghepBan = false;
	JPopupMenu popupMenu = null;
	JTabbedPane panelKVB = null;
	JPanel panelGoiMon = null;
	JMenuItem mntmSDngBn = null;
	JMenuItem mntmKhngSDng = null;
	JMenuItem mntmTchBn = null;
	JMenuItem mntmChiTit = null;
	JMenuItem mntmChuynBn = null;
	public MyJPanelKhuVucBan(KhuVucBan kv, ThongTinGoiMonComponent ttgm, JTabbedPane KVB, JPanel panelGM) {
		
		popupMenu = new JPopupMenu();
		
		mntmSDngBn = new JMenuItem("Sử dụng bàn này");
		mntmSDngBn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				MenuSuDungBan();
			}
		});
		popupMenu.add(mntmSDngBn);
		
		mntmKhngSDng = new JMenuItem("Không sử dụng bàn này");
		mntmKhngSDng.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				MenuKhongSuDungBan();
			}
		});
		popupMenu.add(mntmKhngSDng);
		
		mntmTchBn = new JMenuItem("Tách bàn");
		mntmTchBn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				MenuTachBan();
			}
		});
		popupMenu.add(mntmTchBn);
		
		mntmChiTit = new JMenuItem("Chi tiết");
		mntmChiTit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				MenuChiTiet();
			}
		});
		
		mntmChuynBn = new JMenuItem("Chuyển bàn");
		mntmChuynBn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				ChuyenBan();
			}
		});
		popupMenu.add(mntmChuynBn);
		popupMenu.add(mntmChiTit);
		
		addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent arg0) {
				if(press)
				{
					try {
						int x = (int)arg0.getLocationOnScreen().getX()+2;
						int y = (int)arg0.getLocationOnScreen().getY()+2;
						newitem = LayBanTaiViTriClick(arg0.getPoint());
						
						if(oldItem!= null && newitem!= null){
							if(oldItem.getValue().getMaSo() == newitem.getValue().getMaSo()){
								info.SetInfo("Vui lòng chọn bàn khác");
								info.setSize(200,50);
								ghepBan = false;
							}else{
								info.SetInfo("ghép "+ oldItem.getValue().getTen()+" với "+newitem.getValue().getTen());
								info.setSize(200,50);
								ghepBan = true;
							}
						}else {
							info.SetInfo("Đặt con trỏ vào vị trí bàn muốn ghép với "+oldItem.getValue().getTen());
							info.setSize(350,50);
							ghepBan = false;
						}
						

						info.setLocation(x,y);
						info.show();
					} catch (Exception e) {}
				}
			}
		});
		setBackground(Color.WHITE);
		addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				press = true;
				oldItem = LayBanTaiViTriClick(arg0.getPoint());
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				press = false;
				info.hide();
				GhepBan();
				ghepBan = false;
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				MyItemBan temp = LayBanTaiViTriClick(arg0.getPoint());
				if(temp != null)
				{
					selectedItem = temp;
					SelectItemBan();
					if(arg0.getButton() == MouseEvent.BUTTON1)
						HienThiThongTinGoiMon(selectedItem.getValue());
					else
						showMenu(arg0, popupMenu);
				}
			}
		});

		
		this.value = kv;
		this.ttGoiMon = ttgm;
		this.panelKVB = KVB;
		this.panelGoiMon = panelGM;
		setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
	}
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void GhepBan()
	{
		if(newitem != null && ghepBan)
		{
			if(oldItem.getValue().isSuDung() && newitem.getValue().isSuDung())
			{
				new MyControl().GhepBan(oldItem.getValue(), newitem.getValue(), this, this.panelKVB, this.panelGoiMon);
				this.updateUI();
				//SelectItemBan();
			}else
				JOptionPane.showMessageDialog(this, "Bàn này không được sử dụng!!!","Lỗi",JOptionPane.ERROR_MESSAGE);
		}
	}
	public MyItemBan LayBanTaiViTriClick(Point p)
	{
		try {
			return (MyItemBan)this.getComponentAt(p);
		} catch (Exception e2) {}
		return null;
	}
	
	KhuVucBan value = null;
	public KhuVucBan GetValue()
	{
		return this.value;
	}
	
	//vi tri item duoc chon
	private MyItemBan selectedItem = null;
	public MyItemBan getSelectItem() {
		return selectedItem;
	}
	public void SetSelectedItem(MyItemBan item)
	{
		this.selectedItem = item;
	}
	public void SelectItemBan()
	{
		if(this.selectedItem != null)
		{
			DeSelectAllItem();
			this.selectedItem.Select();
		}
	}
	public void DeSelectAllItem()
	{
		int n = this.getComponentCount();
		for(int i = 0;i<n;i++)
		{
			try {
				MyItemBan item = (MyItemBan)this.getComponent(i);
				item.DeSelect();
			} catch (Exception e){}
		}
	}
	//hien thi thong tin chi tiet goi mon cua ban
	ThongTinGoiMonComponent ttGoiMon = null;
	public void HienThiThongTinGoiMon(Ban b)
	{
		try {
			if(b.isTrangThai())
			{
				Point p = MouseInfo.getPointerInfo().getLocation();
				this.ttGoiMon.setLocation(Common.GetBestLocation(p, (int)this.ttGoiMon.getWidth(), (int)this.ttGoiMon.getHeight()));
				this.ttGoiMon.show();
			}
		} catch (Exception e) {}
	}
	
	public void RemoveAllItem()
	{
		this.removeAll();
		this.selectedItem = null;
		this.oldItem = null;
		this.newitem = null;
	}
	private void showMenu(MouseEvent e, JPopupMenu popup) {
		try {
			popup.show(e.getComponent(), e.getX(), e.getY());
			
			MyItemBan temp = LayBanTaiViTriClick(e.getPoint());
			
			boolean tt = temp.getValue().isTrangThai();
			
			if(!tt){
				boolean sd = temp.getValue().isSuDung();
				this.mntmSDngBn.setEnabled(!sd);
				this.mntmKhngSDng.setEnabled(sd);
				this.mntmChiTit.setEnabled(false);
				this.mntmChuynBn.setEnabled(false);
			}else{
				this.mntmSDngBn.setEnabled(false);
				this.mntmKhngSDng.setEnabled(false);
				this.mntmChiTit.setEnabled(true);
				this.mntmChuynBn.setEnabled(true);
			}
			
			if(temp.getValue().getBanGhep()!=0 && !tt)
				this.mntmTchBn.setEnabled(true);
			else
				this.mntmTchBn.setEnabled(false);
			
		} catch (Exception e2) {
		}
	}

	
	//////////thao tac voi menu chuot phai////////////
	public void MenuSuDungBan()
	{
		try {
			Ban b= this.selectedItem.getValue();
			b.setSuDung(true);
			new BanDAO().saveOrUpdate(b);
			
			this.selectedItem.CapNhatTinhTrang();
		} catch (Exception e) {}
	}
	public void MenuKhongSuDungBan()
	{
		try {
			Ban b= this.selectedItem.getValue();
			b.setSuDung(false);
			new BanDAO().saveOrUpdate(b);
			
			this.selectedItem.CapNhatTinhTrang();
		} catch (Exception e) {}
	}
	public void MenuTachBan()
	{
		try {
			new MyControl().TachBan(this.selectedItem.getValue(), panelKVB, panelGoiMon);
		} catch (Exception ex) {
		}

	}
	public void MenuChiTiet()
	{
		HienThiThongTinGoiMon(this.selectedItem.getValue());
	}
	public void ChuyenBan()
	{
		frmChuyenBan frmCB = new frmChuyenBan(this.selectedItem.getValue().getMaSo());
		if(frmCB.CanShow())
			frmCB.setVisible(true);
	}
}
